package com.itcoon.cloud.framework.enums;

import com.fasterxml.jackson.annotation.JsonValue;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

public interface ValueEnum<V extends Serializable, E extends Enum<E>> {

    @JsonValue
    V value();

    static <B extends ValueEnum<V, E>, V extends Serializable, E extends Enum<E>> B fromValue(V value, Class<B> clazz) {
        return Arrays.stream(clazz.getEnumConstants())
                .filter(e -> Objects.equals(e.value(), value))
                .findFirst().orElse(null);
    }
}
